package com.zenithmind.chat.pojo.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import jakarta.validation.constraints.Min;

/**
 * 消息媒体文件DTO - 遵循单一职责原则
 * 只负责消息媒体文件的数据传输
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@Schema(description = "消息媒体文件DTO")
public class ChatMessageMediaDTO {

    @Schema(description = "消息ID")
    @NotBlank(message = "消息ID不能为空")
    private String messageId;

    @Schema(description = "媒体类型")
    @NotBlank(message = "媒体类型不能为空")
    private String mediaType;

    @Schema(description = "文件名")
    @NotBlank(message = "文件名不能为空")
    @Size(max = 255, message = "文件名长度不能超过255个字符")
    private String fileName;

    @Schema(description = "文件路径")
    @NotBlank(message = "文件路径不能为空")
    private String filePath;

    @Schema(description = "文件URL")
    private String fileUrl;

    @Schema(description = "文件大小（字节）")
    @Min(value = 0, message = "文件大小不能小于0")
    private Long fileSize;

    @Schema(description = "文件扩展名")
    private String fileExtension;

    @Schema(description = "MIME类型")
    private String mimeType;

    @Schema(description = "缩略图路径")
    private String thumbnailPath;

    @Schema(description = "缩略图URL")
    private String thumbnailUrl;

    @Schema(description = "媒体宽度（图片/视频）")
    private Integer width;

    @Schema(description = "媒体高度（图片/视频）")
    private Integer height;

    @Schema(description = "媒体时长（音频/视频，秒）")
    private Integer duration;

    @Schema(description = "媒体描述")
    @Size(max = 500, message = "媒体描述长度不能超过500个字符")
    private String description;

    @Schema(description = "访问权限")
    private String accessLevel;

    @Schema(description = "是否压缩")
    private Boolean isCompressed;

    @Schema(description = "压缩质量")
    private Integer compressionQuality;

    @Schema(description = "扩展信息")
    private String extraInfo;

    /**
     * 验证DTO数据
     * 
     * @return 验证结果
     */
    public boolean isValid() {
        return messageId != null && !messageId.trim().isEmpty() &&
               mediaType != null && !mediaType.trim().isEmpty() &&
               fileName != null && !fileName.trim().isEmpty() &&
               filePath != null && !filePath.trim().isEmpty();
    }

    /**
     * 检查是否为图片类型
     * 
     * @return 是否为图片
     */
    public boolean isImage() {
        if (mediaType == null) {
            return false;
        }
        return mediaType.toLowerCase().startsWith("image");
    }

    /**
     * 检查是否为视频类型
     * 
     * @return 是否为视频
     */
    public boolean isVideo() {
        if (mediaType == null) {
            return false;
        }
        return mediaType.toLowerCase().startsWith("video");
    }

    /**
     * 检查是否为音频类型
     * 
     * @return 是否为音频
     */
    public boolean isAudio() {
        if (mediaType == null) {
            return false;
        }
        return mediaType.toLowerCase().startsWith("audio");
    }

    /**
     * 检查是否为文档类型
     * 
     * @return 是否为文档
     */
    public boolean isDocument() {
        if (mediaType == null) {
            return false;
        }
        String type = mediaType.toLowerCase();
        return type.contains("pdf") || type.contains("doc") || type.contains("txt") ||
               type.contains("excel") || type.contains("ppt");
    }

    /**
     * 获取文件大小的可读格式
     * 
     * @return 可读的文件大小
     */
    public String getReadableFileSize() {
        if (fileSize == null || fileSize == 0) {
            return "0 B";
        }
        
        String[] units = {"B", "KB", "MB", "GB", "TB"};
        int unitIndex = 0;
        double size = fileSize.doubleValue();
        
        while (size >= 1024 && unitIndex < units.length - 1) {
            size /= 1024;
            unitIndex++;
        }
        
        return String.format("%.1f %s", size, units[unitIndex]);
    }

    /**
     * 获取媒体类型的显示名称
     * 
     * @return 媒体类型显示名称
     */
    public String getMediaTypeDisplayName() {
        if (isImage()) {
            return "图片";
        } else if (isVideo()) {
            return "视频";
        } else if (isAudio()) {
            return "音频";
        } else if (isDocument()) {
            return "文档";
        } else {
            return "文件";
        }
    }

    /**
     * 检查是否有缩略图
     * 
     * @return 是否有缩略图
     */
    public boolean hasThumbnail() {
        return (thumbnailPath != null && !thumbnailPath.trim().isEmpty()) ||
               (thumbnailUrl != null && !thumbnailUrl.trim().isEmpty());
    }

    /**
     * 检查是否有尺寸信息
     * 
     * @return 是否有尺寸信息
     */
    public boolean hasDimensions() {
        return width != null && height != null && width > 0 && height > 0;
    }

    /**
     * 检查是否有时长信息
     * 
     * @return 是否有时长信息
     */
    public boolean hasDuration() {
        return duration != null && duration > 0;
    }

    /**
     * 获取时长的可读格式
     * 
     * @return 可读的时长
     */
    public String getReadableDuration() {
        if (!hasDuration()) {
            return "00:00";
        }
        
        int minutes = duration / 60;
        int seconds = duration % 60;
        
        if (minutes >= 60) {
            int hours = minutes / 60;
            minutes = minutes % 60;
            return String.format("%02d:%02d:%02d", hours, minutes, seconds);
        } else {
            return String.format("%02d:%02d", minutes, seconds);
        }
    }

    /**
     * 获取尺寸的显示格式
     * 
     * @return 尺寸显示格式
     */
    public String getDimensionDisplay() {
        if (!hasDimensions()) {
            return "";
        }
        return width + "×" + height;
    }

    /**
     * 检查是否被压缩
     * 
     * @return 是否被压缩
     */
    public boolean isCompressed() {
        return isCompressed != null && isCompressed;
    }

    /**
     * 获取压缩质量的显示名称
     * 
     * @return 压缩质量显示名称
     */
    public String getCompressionQualityName() {
        if (compressionQuality == null) {
            return "原始";
        }
        if (compressionQuality >= 90) {
            return "高质量";
        } else if (compressionQuality >= 70) {
            return "中等质量";
        } else {
            return "低质量";
        }
    }
}
